bitsPerIter = 247;
n = 255;
codeRate = 247/255;
m = 8; 
b= ceil(log2(m));
k=bitsPerIter;

for EbNo = 1:1

    adjSNR = EbNo - 10*log10(1/codeRate) + 10*log10(b);
    msg_orig = randint(1, bitsPerIter, 256);
    mi = m2r(de2bi(msg_orig,8,'left-msb'));
    % Reed Solomon encoding the message, saving encoder state between iterations
    msg  = gf(msg_orig,m);      % Two k-symbol message words
    msg_enc_bi = rsenc(msg,n,k);

    msg_tx = m2r(de2bi(msg_enc_bi.x,8,'left-msb'));
   
    % Use this set pf codes for 8-PPM or M-PPM
    msg_ppm = ppm_mod(4,msg_tx)
    msg_tx1=apply_lognormal(0.1,msg_ppm)   
    msg_rx = awgn(msg_tx1, adjSNR, 'measured', [], 'dB')
    msg_ppm_demod = ppm_demod(4, msg_rx)
    for i = 1:2040
        if msg_ppm_demod(i)>1
        msg_ppm_demod(i) = 1
        end
    end
    mx = r2m(msg_ppm_demod)

%     msg_ook = ook_mod(msg_tx,10);
%     % Add Gaussian noise to the signal.
%     msg_tx1=apply_lognormal(0.01,msg_ook);
%     msg_rx = awgn(msg_tx1, adjSNR, 'measured', [], 'dB');
%     
%     msg_ook_demod = ook_demod(msg_rx,10);
%     mx = r2m(msg_ook_demod);
    msg_dec = rsdec(gf(mx,8),n,k);
    mo = m2r(de2bi(msg_dec.x,8,'left-msb'));
    % Accumulate bit count and bit error statistics after each iteration
    berror = biterr(mi,mo)
end

    